package httpService

import (
	"github.com/labstack/echo/v4"
	"pm/db"
	"pm/model"
	"strconv"
)

func PackageConfigUpdate(c echo.Context)error{
	name := c.FormValue("name")
	ptype := c.FormValue("type")
	args := c.FormValue("args")
	path := c.FormValue("path")
	idStr := c.FormValue("id")
	servers := c.FormValue("servers")
	id,_ := strconv.Atoi(idStr)
	if name == "" || path == ""{
		return c.JSON(200, map[string]interface{}{
			"status": 1,
			"msg":"参数错误",
		})
	}
	if id <= 0{
		return c.JSON(200, map[string]interface{}{
			"status": 2,
			"msg":"参数错误",
		})
	}
	var exists model.Package
	if err := db.NewDb().Where("name = ?", name).Take(&exists).Error;err ==nil{
		if exists.ID != uint(id){
			return c.JSON(200, map[string]interface{}{
				"status": 1,
				"msg":"包名重复",
			})
		}
	}
	ptypeNum,_ := strconv.Atoi(ptype)
	if ptypeNum == 0{
		ptypeNum = 1
	}
	packageData := model.Package{
		Name: name,
		Type: ptypeNum,
		Path: path,
		Args: args,
		Servers: servers,
	}
	if err := db.NewDb().Where("ID =?", id).Updates(&packageData).Error;err !=nil{
		return c.JSON(200, map[string]interface{}{
			"status": 1,
			"msg":"系统更新失败",
		})
	}else{
		return c.JSON(200, map[string]interface{}{
			"status": 0,
			"msg":"",
		})
	}
}


